#!/bin/sh
# PCP QA Test No. 1573
# Exercise libpcp_web memory leak without a redis-server.
#
# Copyright (c) 2020 Red Hat.
#

seq=`basename $0`
echo "QA output created by $seq"

# get standard environment, filters and checks
. ./common.product
. ./common.filter
. ./common.check

_check_series

_cleanup()
{
    cd $here
    if $need_restore
    then
	need_restore=false
	_service pmlogger stop >/dev/null
	$sudo rm -rf $PCP_LOG_DIR/pmlogger
	$sudo mv $PCP_LOG_DIR/pmlogger.$seq $PCP_LOG_DIR/pmlogger
	_restore_config $PCP_ETC_DIR/pcp/pmlogger
	_service pcp restart 2>&1 | _filter_pcp_stop | _filter_pcp_start
	_wait_for_pmcd
	_wait_for_pmlogger
	_restore_config $PCP_SYSCONF_DIR/pmproxy
    fi
    if $pmproxy_was_running
    then
	echo "Restart pmproxy ..." >>$here/$seq.full
	_service pmproxy restart >>$here/$seq.full 2>&1
	_wait_for_pmproxy
    else
	echo "Stopping pmproxy ..." >>$here/$seq.full
	_service pmproxy stop >>$here/$seq.full 2>&1
    fi
    $sudo rm -rf $tmp $tmp.*
}

status=1	# failure is the default!
need_restore=false
$sudo rm -rf $tmp $tmp.* $seq.full
trap "_cleanup; exit \$status" 0 1 2 3 15

pmproxy_was_running=false
[ -f $PCP_RUN_DIR/pmproxy.pid ] && pmproxy_was_running=true
echo "pmproxy_was_running=$pmproxy_was_running" >>$here/$seq.full

# real QA test starts here
_save_config $PCP_SYSCONF_DIR/pmproxy
need_restore=true

# only want the primary logger running
_save_config $PCP_ETC_DIR/pcp/pmlogger
_restore_pmlogger_control

#$sudo rm -f $PCP_SYSCONF_DIR/pmproxy/*
echo "[pmproxy]" > $tmp.conf
echo "pcp.enabled = true" >> $tmp.conf
echo "http.enabled = true" >> $tmp.conf
echo "redis.enabled = true" >> $tmp.conf
echo "[discover]" >> $tmp.conf
echo "enabled = true" >> $tmp.conf
echo "[pmseries]" >> $tmp.conf
echo "enabled = false" >> $tmp.conf
$sudo cp $tmp.conf $PCP_SYSCONF_DIR/pmproxy/pmproxy.conf

_service pmlogger stop >/dev/null

# move aside existing logs so we can measure base memory footprint
[ -d $PCP_LOG_DIR/pmlogger.$seq ] && $sudo mv $PCP_LOG_DIR/pmlogger.$seq $PCP_LOG_DIR/pmlogger.$seq.saved
$sudo mv $PCP_LOG_DIR/pmlogger $PCP_LOG_DIR/pmlogger.$seq
$sudo mkdir -p $PCP_LOG_DIR/pmlogger
$sudo chmod 775 $PCP_LOG_DIR/pmlogger
$sudo chown $PCP_USER:$PCP_USER $PCP_LOG_DIR/pmlogger

_service pmproxy restart 2>&1 | _filter_pcp_stop | _filter_pcp_start
_wait_for_pmproxy

pmproxy_pid=`_get_pids_by_name -a pmproxy`
[ -z "$pmproxy_pid" ] && echo === pmproxy not running && status=1 && exit 1
echo === extract initial rss
pmproxy_rss1=`pminfo -f proc.memory.rss |
	$PCP_AWK_PROG '{ if ($2 == "['$pmproxy_pid'") { print $NF} }'`

echo === restarting pmlogger # primary only
_service pmlogger restart 2>&1 | _filter_pcp_start
_wait_for_pmlogger

echo === wait for pmproxy to process filesystem events
pmsleep 4.2

echo === extract updated rss
pmproxy_rss2=`pminfo -f proc.memory.rss |
	$PCP_AWK_PROG '{ if ($2 == "['$pmproxy_pid'") { print $NF} }'`

echo === checking rss within tolerance
_within_tolerance "rss" $pmproxy_rss1 $pmproxy_rss2 10%
[ $pmproxy_rss2 -gt 10000 ] && echo "Unexpected pmproxy RSS: $pmproxy_rss2, was initially $pmproxy_rss1"

echo "RSS1 for PID $pmproxy_pid is $pmproxy_rss1" >> $here/$seq.full
echo "RSS2 for PID $pmproxy_pid is $pmproxy_rss2" >> $here/$seq.full
cat $PCP_LOG_DIR/pmproxy/pmproxy.log >>$seq.full
echo === see $seq.full for pmproxy rss and logs

# success, all done
status=0
exit
